//
//  ActivityDetailViewController.m
//  EasyToTesco
//
//  Created by mc on 16/2/25.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "ActivityDetailViewController.h"
#import "HomeTableViewCell.h"

#import "HeaderFileForEasyToTesco.h"

#import "SDRefresh.h"

@interface ActivityDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *actDetailtableview;

//
@property (nonatomic, strong)SDRefreshFooterView *refreshFooter;
@end

@implementation ActivityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self activityDetailInitSomething];
    
    [self activityDetailUI];
    
    [self activityDetailTableview];
    
}

- (void)activityDetailInitSomething {

}

- (void)activityDetailUI {
    [self addActivityDetailTopview];
    
    [self setupactdecailHeader];
    [self setupactdetailFooter];
}

- (void)activityDetailTableview {
    self.actDetailtableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.9) style:UITableViewStylePlain];
    self.actDetailtableview.dataSource = self;
    self.actDetailtableview.delegate = self;
    self.actDetailtableview.tableFooterView = [[UIView alloc] init];
    self.actDetailtableview.rowHeight = 100;
    [self.view addSubview:self.actDetailtableview];
}


- (void)addActivityDetailTopview {
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"活动详情";
}

//返回上一级页面
- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"HomeTableViewCell";
        HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [self setLeftViewWithTextField:cell.location imageName:@"Icon_-Location-pin.png"];
            [self setLeftViewWithTextField:cell.NumOfBuy imageName:@"men-gray-icon.png"];
        }
    return cell;
}
#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


/**
 *  给UITextField设置右侧的图片
 *
 *  @param textField UITextField
 *  @param imageName 图片名称
 */

-(void)setLeftViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    
    UIImageView *rightView = [[UIImageView alloc]init];
    
    rightView.image = [UIImage imageNamed:imageName];
    rightView.frame = CGRectMake(0, 0, CGRectGetHeight(textField.frame), CGRectGetHeight(textField.frame));
    rightView.contentMode = UIViewContentModeCenter;
    textField.leftView = rightView;
    textField.leftViewMode = UITextFieldViewModeAlways;
}

- (void)setupactdecailHeader
{
    //添加下拉刷新对象
    SDRefreshHeaderView *refreshHeader = [SDRefreshHeaderView refreshView];
    
//     默认是在navigationController环境下，如果不是在此环境下，请设置
    refreshHeader.isEffectedByNavigationController = NO;
    
    //加入到目标tableview上
    [refreshHeader addToScrollView:self.actDetailtableview];
    
    //添加下拉加载内容的方法
    __weak SDRefreshHeaderView *weakRefreshHeader = refreshHeader;
    __weak typeof(self) weakSelf = self;
    refreshHeader.beginRefreshingOperation = ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
#pragma mark -- 加载数据的方法
            
            [weakSelf.actDetailtableview reloadData];
            [weakRefreshHeader endRefreshing];
        });
    };
    
    // 进入页面自动加载一次数据
    [refreshHeader autoRefreshWhenViewDidAppear];
}

- (void)setupactdetailFooter
{
    SDRefreshFooterView *refreshFooter = [SDRefreshFooterView refreshView];
    refreshFooter.isEffectedByNavigationController = NO;
    [refreshFooter addToScrollView:self.actDetailtableview];
    [refreshFooter addTarget:self refreshAction:@selector(ActDetailfooterRefresh)];
    _refreshFooter = refreshFooter;
}


- (void)ActDetailfooterRefresh
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
#pragma mark -- 加载数据的方法
        
        [self.actDetailtableview reloadData];
        [self.refreshFooter endRefreshing];
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
